﻿using _01Net8DependencyInject.Interface;
using _01Net8DependencyInject.Model;
using Autofac;
using Microsoft.Extensions.DependencyInjection;
using System.ComponentModel.Design;
using System.Reflection;
using static System.Formats.Asn1.AsnWriter;

namespace _01Net8DependencyInject
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            #region 单例
            //{
            //    Console.WriteLine("依赖注入单例");
            //    ServiceCollection service = new ServiceCollection();

            //    service.AddTransient<ProductService>();
            //    service.AddSingleton<ProductRepository>();
            //    IServiceProvider provider = service.BuildServiceProvider();

            //    ProductService productService = provider.GetRequiredService<ProductService>();
            //    ProductService productService1 = provider.GetRequiredService<ProductService>();
            //    productService.GetProduct();
            //    productService1.GetProduct();
            //}
            #endregion

            #region 瞬时
            //{
            //    Console.WriteLine("依赖注入瞬时");
            //    ServiceCollection service = new ServiceCollection();

            //    service.AddTransient<ProductService>();
            //    service.AddTransient<ProductRepository>();

            //    IServiceProvider provider = service.BuildServiceProvider();

            //    ProductService productService = provider.GetRequiredService<ProductService>();
            //    ProductService productService1 = provider.GetRequiredService<ProductService>();
            //    productService.GetProduct();
            //    productService1.GetProduct();
            //}
            #endregion

            #region Scope
            //{
            //    Console.WriteLine("依赖注入 Scope");
            //    ServiceCollection service = new ServiceCollection();

            //    service.AddScoped<ProductService>();
            //    service.AddScoped<ProductRepository>();

            //    IServiceProvider provider = service.BuildServiceProvider();

            //    using (var scope = provider.CreateScope())
            //    {

            //        ProductService productService = scope.ServiceProvider.GetRequiredService<ProductService>();
            //        ProductService productService1 = scope.ServiceProvider.GetRequiredService<ProductService>();
            //        productService.GetProduct();
            //        productService1.GetProduct();
            //    }

            //    using (var scope = provider.CreateScope())
            //    {

            //        ProductService productService = scope.ServiceProvider.GetRequiredService<ProductService>();
            //        ProductService productService1 = scope.ServiceProvider.GetRequiredService<ProductService>();
            //        productService.GetProduct();
            //        productService1.GetProduct();
            //    }

            //    using (ServiceProvider provider1 = service.BuildServiceProvider())
            //    {

            //        ProductService productService = provider1.GetRequiredService<ProductService>();
            //        ProductService productService1 = provider1.GetRequiredService<ProductService>();
            //        productService.GetProduct();
            //        productService1.GetProduct();
            //    }

            //}
            #endregion


            #region 瞬时  接口注册  + 多实现注册
            //{
            //    Console.WriteLine("依赖注入 Scope");
            //    ServiceCollection service = new ServiceCollection();

            //    service.AddScoped<IProductService, ProductService>();
            //    service.AddScoped<IProductRepository, ProductRepository>();
            //    service.AddScoped<IProductRepository, ProductRepositoryCS>(); // 注册多个继承同一个接口的类，默认项目目录最后一个

            //    IServiceProvider provider = service.BuildServiceProvider();

            //    using (var scope = provider.CreateScope())
            //    {
            //        //IProductService productService = scope.ServiceProvider.GetRequiredService<IProductService>();
            //        //IProductService productService1 = scope.ServiceProvider.GetRequiredService<IProductService>();
            //        //productService.GetProduct();
            //        //productService1.GetProduct();

            //        IEnumerable<IProductRepository> productRepositories = scope.ServiceProvider.GetServices<IProductRepository>();
            //        foreach (var productRepositorie in productRepositories)
            //        {
            //            productRepositorie.GetProductRepository();
            //        }
            //    }
            //}
            #endregion

            #region 5、Autofac 属性注册 添加了PropertiesAutowired
            //{
            //    Console.WriteLine("依赖注入 Scope");
            //    ContainerBuilder container = new ContainerBuilder();

            //    container.RegisterType<ProductService>().As<IProductService>().PropertiesAutowired();
            //    container.RegisterType<ProductRepository>().As<IProductRepository>().PropertiesAutowired();
            //    container.RegisterType<ProductRepositoryCS>().As<IProductRepository>().PropertiesAutowired();


            //    var provider = container.Build();

            //    using (var scope = provider.BeginLifetimeScope())
            //    {
            //        IProductService productService = scope.Resolve<IProductService>();
            //        IProductService productService1 = scope.Resolve<IProductService>();
            //        productService.GetProduct();
            //        productService1.GetProduct();
            //    }
            //}
            #endregion

            #region 6、Autofac 批量注册
            {
                Console.WriteLine("Autofac 批量注册 Scope");
                ContainerBuilder container = new ContainerBuilder();

                container.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())  // Assembly.GetExecutingAssembly() 加载整个程序集
                    .AsImplementedInterfaces()   // 只要有接口都实现注册
                    .PropertiesAutowired();    // 属性注册

                var provider = container.Build();

                using (var scope = provider.BeginLifetimeScope())
                {
                    IProductService productService = scope.Resolve<IProductService>();
                    IProductService productService1 = scope.Resolve<IProductService>();
                    productService.GetProduct();
                    productService1.GetProduct();
                }
            }
            #endregion

            #region 7、Autofac 批量注册 接口过滤
            //{
            //    Console.WriteLine("Autofac 批量注册 Scope");
            //    ContainerBuilder container = new ContainerBuilder();

            //    container.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())  // Assembly.GetExecutingAssembly() 加载整个程序集
            //        .Where(T => !T.GetInterfaces().Contains(typeof(IInject)))  // 进行过滤
            //        .AsImplementedInterfaces()
            //        .PropertiesAutowired();    // 属性注册


            //    var provider = container.Build();

            //    using (var scope = provider.BeginLifetimeScope())
            //    {
            //        IProductService productService = scope.Resolve<IProductService>();
            //        IProductService productService1 = scope.Resolve<IProductService>();
            //        productService.GetProduct();
            //        productService1.GetProduct();
            //    }
            //}
            #endregion


            #region 8、Autofac 批量注册 特性过滤
            //{
            //    Console.WriteLine("Autofac 批量注册 Scope");
            //    ContainerBuilder container = new ContainerBuilder();

            //    container.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())  // Assembly.GetExecutingAssembly() 加载整个程序集
            //        .Where(T => !T.GetCustomAttributes<MyAttribute>().Any())  // 进行过滤
            //        .AsImplementedInterfaces()
            //        .PropertiesAutowired();    // 属性注册


            //    var provider = container.Build();

            //    using (var scope = provider.BeginLifetimeScope())
            //    {
            //        IProductService productService = scope.Resolve<IProductService>();
            //        IProductService productService1 = scope.Resolve<IProductService>();
            //        productService.GetProduct();
            //        productService1.GetProduct();
            //    }
            //}
            #endregion
        }
    }
}
